using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MVCWeb.Models;
using MVCWeb.Models.ViewData;

namespace MVCWeb.Controllers
{
    public class MenuController : BaseController
    {

        //
        // GET: /Menu/

        [Authorize(Roles = "Administrators")]
        public ActionResult Index()
        {
            IList<Menu> finalMenus = new List<Menu>();

            IList<Menu> allMenus = dc.Menus.Where(m => m.CommunityId == null || m.CommunityId == 0).OrderBy(m => m.MenuName).ToList();
            IList<Menu> topMenus = allMenus.Where(m => (m.ParentMenuId == 0 || m.ParentMenuId.HasValue == false)).OrderBy(m => m.MenuName).ToList();

            foreach (var item in topMenus)
            {
                item.MenuName = "<b>" + item.MenuName + "</b>";
                finalMenus.Add(item);
                Menu menu = item;
                IList<Menu> subMenus = allMenus.Where(m => m.ParentMenuId == menu.MenuId).OrderBy(m => m.MenuName).ToList();
                foreach (var subitem in subMenus)
                {
                    subitem.MenuName = "-- " + subitem.MenuName;
                    finalMenus.Add(subitem);
                }
            }
            return View(finalMenus);
        }

        
        //
        // GET: /Menu/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            MenuNewViewData data = new MenuNewViewData();
            data.Menu = new Menu();
            data.Parents = this.dc.Menus.Where(m => (m.CommunityId == 0 || m.CommunityId == null) && (m.ParentMenuId == 0 || m.ParentMenuId == null)).OrderBy(x => x.MenuName).ToList();
            var types = from e in this.dc.Menus
                        group e by e.Type into g
                        select g.First();
            data.Types = types.Where(m => m.Type != "CommunityMenu").ToList();

            return View(data);
        }

        //
        // POST: /Menu/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Create(Menu menu, FormCollection collection)
        {
            try
            {
                if (!(collection["IsRoot"]).Equals("false"))
                {
                    menu.ParentMenuId = 0;
                }

                if (!(string.IsNullOrEmpty(collection["NewType"])))
                {
                    menu.Type = Convert.ToString(collection["NewType"]);
                }

                dc.Menus.InsertOnSubmit(menu);
                dc.SubmitChanges();

                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                return Redirect(Request.RawUrl);
            }
            catch
            {
                return View(menu);
            }
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            MenuNewViewData data = new MenuNewViewData();
            data.Menu = dc.Menus.Single(x => x.MenuId == id);
            data.Parents = this.dc.Menus.Where(m => (m.CommunityId == 0 || m.CommunityId == null) && (m.ParentMenuId == 0 || m.ParentMenuId == null)).OrderBy(x => x.MenuName).ToList();
            var types = from e in this.dc.Menus
                        group e by e.Type into g
                        select g.First();
            data.Types = types.ToList();
            return View(data);
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, Menu menu, FormCollection collection)
        {
            try
            {
                Menu dbMemu = dc.Menus.First(x => x.MenuId == id);
                if (!(collection["IsRoot"]).Equals("false"))
                {
                    menu.ParentMenuId = 0;
                }

                if (!(String.IsNullOrEmpty(collection["NewType"])))
                {
                    menu.Type = Convert.ToString(collection["NewType"]);
                }
                dbMemu.MenuName = menu.MenuName;
                dbMemu.ParentMenuId = menu.ParentMenuId;
                dbMemu.Link = menu.Link;
                dbMemu.SortOrder = menu.SortOrder;
                dbMemu.Type = menu.Type;
                dbMemu.Desc = menu.Desc;
                dbMemu.CommunityId = null;
                dbMemu.Active = menu.Active;

                dc.SubmitChanges();

                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                return Redirect(Request.RawUrl);
            }
            catch
            {
                MenuNewViewData data = new MenuNewViewData();
                data.Menu = dc.Menus.Single(x => x.MenuId == id);
                var types = from e in this.dc.Menus
                            group e by e.Type into g
                            select g.First();
                data.Types = types.ToList();
                return View(data);
            }
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            try
            {
                Menu menu = dc.Menus.First(x => x.MenuId == id);
                dc.Menus.DeleteOnSubmit(menu);
                dc.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
    }
}
